package cn.com.checne.controller.superManager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.checne.constants.Constants;
import cn.com.checne.dao.ContentInfoDao;
import cn.com.checne.dao.UserInfoDao;
import cn.com.checne.dao.WebsiteInfoDao;
import cn.com.checne.model.domain.ContentInfo;
import cn.com.checne.model.domain.UserInfo;
import cn.com.checne.model.domain.WebsiteInfo;
import cn.com.checne.util.PageBean;
import cn.com.checne.util.StringTools;
import cn.com.checne.util.WebUtils;

@Controller
@RequestMapping(value = "/super/content")
public class ContentInfoManagerController {

    protected static final Logger logger = Logger.getLogger(ContentInfoManagerController.class);
    @Resource
	private ContentInfoDao contentDao;

	@Resource
	private WebsiteInfoDao websiteInfoDao;

	@Resource
	private UserInfoDao userInfoDao;


    //内容管理--内容--列举所有
    @RequestMapping(value = "/content-list.do")
    public ModelAndView contentList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
    	
    	ModelAndView mav = new ModelAndView("super/synch/content-list");
		int curPage = WebUtils.getRequestParameterAsInt(request, "curPage", 1);
		String kw = WebUtils.getRequestParameterAsString(request, "kw");
        kw = StringTools.converISO2UTF8(kw);
		int websiteId = WebUtils.getRequestParameterAsInt(request, "websiteId",0);
		int userId = WebUtils.getRequestParameterAsInt(request, "userId", 0);
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		ContentInfo query = new ContentInfo();
		query.setKw(kw);
		query.setWebsiteId(websiteId);
		query.setUserId(userId);
		List<ContentInfo> contentInfoList = contentDao.getSubList(query,pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(contentDao.getCount(query));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		List<UserInfo> userInfoList = userInfoDao.getUserListByRoleId(-1, 0,Integer.MAX_VALUE);
		List<WebsiteInfo> websiteInfoList = websiteInfoDao.getSubList(null, 0,Integer.MAX_VALUE);
		modelMap.put("contentInfoList", contentInfoList);
		modelMap.put("pageBean", pageBean);
		modelMap.put("content", query);
		modelMap.put("userInfoList", userInfoList);
		modelMap.put("websiteInfoList", websiteInfoList);
		mav.addAllObjects(modelMap);
		return mav;
		
    }

    //内容管理--内容新增、修改页面
    @RequestMapping(value = "/content-show.do")
    public ModelAndView contentShow(HttpServletRequest request, HttpServletResponse response, HttpSession session, ModelMap modelMap) throws Exception {
        ModelAndView mav = new ModelAndView("super/synch/content-show");
		ContentInfo content = new ContentInfo();
		content.setPublishTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
		String contentId = WebUtils.getRequestParameterAsString(request,"contentId");
        String opt = "create";
		if(StringUtils.isNotBlank(contentId)){
			content = contentDao.getContentById(contentId);
            opt = "update";
		}
		List<WebsiteInfo> websiteInfoList = websiteInfoDao.getSubList(null, 0,Integer.MAX_VALUE);
		List<UserInfo> userInfoList = userInfoDao.getUserListByRoleId(0, 0,Integer.MAX_VALUE);
        modelMap.put("opt", opt);
		modelMap.put("content", content);
		modelMap.put("websiteInfoList", websiteInfoList);
		modelMap.put("userInfoList", userInfoList);
		modelMap.put("userId", Integer.valueOf(((UserInfo) session.getAttribute(Constants.SESSIONNAME)).getId()));
		mav.addAllObjects(modelMap);
        return mav;
    }

    //内容管理--内容新增
    @RequestMapping(value = "/content-create.do")
    public ModelAndView contentCreate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("common/message");
        String message = "";
       
        String contentId = cn.com.checne.util.StringTools.getUUID();
		String title = WebUtils.getRequestParameterAsString(request, "title");
		int websiteId = 0;
		List<WebsiteInfo> websiteInfoList = websiteInfoDao.getSubList(null, 0,Integer.MAX_VALUE);
		for (WebsiteInfo website : websiteInfoList) {
			if (website.getIsSelf() == 1) {
				websiteId = website.getId();
			}
		}
		String text = WebUtils.getRequestParameterAsString(request, "text");
		String publishTime = WebUtils.getRequestParameterAsString(request,"publishTime");
		int userId = WebUtils.getRequestParameterAsInt(request, "userId", 0);

		if (StringUtils.isBlank(text)) {
			modelMap.put("message", "新闻内容不能为空!");
			mav.addAllObjects(modelMap);
			return mav;
		}

		ContentInfo content = new ContentInfo();
		content.setContentId(contentId);
		content.setTitle(title);
		content.setWebsiteId(websiteId);
		content.setText(text);
		content.setPublishTime(publishTime);
		content.setUserId(userId);
		boolean flag = contentDao.insert(content);
		if (flag)
			message = "新闻信息添加完毕!";
		else {
			message = "新闻信息添加失败!";
		}
		modelMap.put("message", message);
		mav.addAllObjects(modelMap);
		
        return mav;
    }

    //内容管理--内容修改
    @RequestMapping(value = "/content-update.do")
    public ModelAndView contentUpdate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView("common/message");
        String message = "";
        String contentId = WebUtils.getRequestParameterAsString(request,"contentId");
		String title = WebUtils.getRequestParameterAsString(request, "title");
		int websiteId = 0;
		List<WebsiteInfo> websiteInfoList = websiteInfoDao.getSubList(null, 0,Integer.MAX_VALUE);
		for (WebsiteInfo website : websiteInfoList) {
			if (website.getIsSelf() == 1) {
				websiteId = website.getId();
			}
		}
		String text = WebUtils.getRequestParameterAsString(request, "text");
		String publishTime = WebUtils.getRequestParameterAsString(request,
				"publishTime");
		int userId = WebUtils.getRequestParameterAsInt(request, "userId", 0);
		
		if (StringUtils.isBlank(text)) {
			modelMap.put("message", "新闻内容不能为空!");
			mav.addAllObjects(modelMap);
			return mav;
		}
		
		ContentInfo content = contentDao.getContentById(contentId);
		content.setTitle(title);
		content.setWebsiteId(websiteId);
		content.setText(text);
		content.setPublishTime(publishTime);
		content.setUserId(userId);
		
		boolean flag = contentDao.update(content);
		if (flag)
			message = "新闻信息更新完毕!";
		else {
			message = "新闻信息更新失败!";
		}
		modelMap.put("message", message);
		mav.addAllObjects(modelMap);
        return mav;
    }

    //内容管理--内容删除
    @RequestMapping(value = "/content-del.do")
    public ModelAndView contentDel(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView("common/message");
        String message = "内容删除成功!";
        String contentIds = WebUtils.getRequestParameterAsString(request,"contentId");
		String[] idArr = StringUtils.split(contentIds, ",");
		try {
			for (String contentId : idArr) {
				contentDao.del(contentId);
			}
			message = "新闻信息删除完毕!";
		} catch (Exception e) {
			message = "新闻信息删除失败!";
			logger.error("新闻信息删除失败! ids=" + contentIds, e);
		}
		modelMap.put("message", message);
		mav.addAllObjects(modelMap);
        return mav;
    }

    @RequestMapping(value="/content-showSynch.do")
	public ModelAndView showContentSynch(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("super/synch/content-showSynch");
		String contentIds = WebUtils.getRequestParameterAsString(request,"contentId");
		if (StringUtils.isBlank(contentIds)) {
			map.put("message", "请选择要同步的文章!");
			mav.addAllObjects(map);
			mav.setViewName("common/message");
			return mav;
		}
		List<ContentInfo> contentInfoList = new ArrayList<ContentInfo>();
		String[] ids = contentIds.split(",");
		ContentInfo content = null;
		for (String contentId : ids) {
			content = contentDao.getContentById(contentId);
			contentInfoList.add(content);
		}
		List<WebsiteInfo> websiteInfoList = websiteInfoDao.getSubList(null, 0,
				2147483647);
		map.put("websiteInfoList", websiteInfoList);
		map.put("contentInfoList", contentInfoList);
		mav.addAllObjects(map);
		return mav;
	}
}